package com.tadpole.music.view.activity.question;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.tadpole.music.R;
import com.tadpole.music.config.Constant;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.StatusBarUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.activity.study.MyErrorTopicActivity;
import com.tadpole.music.view.widget.NiceImageView;

import java.util.List;
import java.util.Objects;

/**
 * 模拟结果页面
 * Created By 孙岩
 * on 2021/1/9
 */
public class QuestionResultActivity extends BaseActivity {
    //回退
    private View view_back_icon;
    //头像
    private NiceImageView ivImage;
    //得分
    private TextView tvFen;
    //正确率
    private TextView tvZhengQueLv;
    //答对数
    private TextView tvZhengQue;
    //时长
    private TextView tvTime;
    //查看错题
    private TextView tvShowError;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question_result);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //将头部状态栏置为透明
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null);
        List<Integer> list = getIntent().getIntegerArrayListExtra("info");
        String time = getIntent().getStringExtra("time");
        initViews();
        initListeners();
        initData(list,time);

    }

    //初始化控件
    private void initViews(){
        view_back_icon = findViewById(R.id.view_back_icon);
        ivImage = findViewById(R.id.ivImage);
        tvFen = findViewById(R.id.tvFen);
        tvZhengQueLv = findViewById(R.id.tvZhengQueLv);
        tvZhengQue = findViewById(R.id.tvZhengQue);
        tvTime = findViewById(R.id.tvTime);
        tvShowError = findViewById(R.id.tvShowError);
    }

    //初始化监听器
    private void initListeners(){
        view_back_icon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                QuestionResultActivity.this.finish();
            }
        });

        tvShowError.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
                    if(SpUtil.getInstance(QuestionResultActivity.this).getString(Constant.TOKEN,"").equals("")){
                        Intent intent = new Intent(QuestionResultActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }else{
                        Intent intent = new Intent(QuestionResultActivity.this, MyErrorTopicActivity.class);
                        startActivity(intent);
                    }
                }else{
                    ToastUtils.show(getResources().getString(R.string.net_work));
                }
            }
        });
    }

    //初始化数据
    @SuppressLint("SetTextI18n")
    private void initData(List<Integer> list, String time){
        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.mipmap.app_icon)
                .error(R.mipmap.app_icon)
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);

        if (SpUtil.getInstance(QuestionResultActivity.this).getString(Constant.USER_AVATAR, "").equals("")) {
            Glide.with(Objects.requireNonNull(QuestionResultActivity.this)).load(R.mipmap.app_icon).apply(requestOptions).into(ivImage);
        } else {
            Glide.with(Objects.requireNonNull(QuestionResultActivity.this)).load(SpUtil.getInstance(QuestionResultActivity.this).getString(Constant.USER_AVATAR, "")).apply(requestOptions).into(ivImage);
        }

        if(list!=null&&list.size()!=0){
            tvZhengQueLv.setText((100-(list.size()*10))+"%");
            tvZhengQue.setText((10-list.size())+"道");
            tvFen.setText(((10-list.size())*10)+"分");
        }

        tvTime.setText(time+"分钟");
    }
}
